package game.client;

import game.Card;
import game.Player;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Iterator;

public class Table implements Serializable {
	private static final long serialVersionUID = 5521289764267367496L;
	private Player player;
	protected ArrayList<Card> cards;
	protected double betOnTable;
	protected Card betLock = new Card("X",1);
	private ArrayList<Player> opponents;
	/**
	 * Constructs new table with specified player behind table.
	 * @param player
	 */
	public Table(Player player) {
		this();
		this.player = player;
	}
	/**
	 * Constructs table with an empty cards and opponents array.
	 * Sets betOnTable to 0.
	 */
	public Table() {
		cards = new ArrayList<Card>(3);
		betOnTable = 0;
		opponents = new ArrayList<Player>();
	}
	/**
	 * @return the betOnTable
	 */
	public double getBetOnTable() {
		synchronized (betLock) {
			return betOnTable;
		}
	}
	/**
	 * Removes all cards from the table.
	 */
	public void clearCardsOnTable() {
		synchronized (cards) {
			cards.removeAll(cards);
		}
	}
	/**
	 * Sets bets on tabel to 0.
	 */
	public void clearBetsOnTable() {
		synchronized (betLock) {
			betOnTable = 0;
		}
	}
	
	/**
	 * Sets opponents to the table.
	 * @param opponents the opponents to set
	 */
	public void setOpponents(ArrayList<Player> opponents) {
		this.opponents = opponents;			
	}
	/**
	 * Remove opponent from table.
	 * @param name opponent to remove
	 */
	public void removeOpponent(Player name) {
		opponents.remove(name);			
	}
	/**
	 * Add opponent to table.
	 * @param name opponent to add
	 */
	public void addOpponent(Player name) {
		opponents.add(name);			

	}
	/**
	 * @return the opponents
	 */
	public ArrayList<Player> getOpponents() {
		return opponents;			

	}

	/**
	 * @param betOnTable the betOnTable to set
	 */
	public void addBetToTable(double betOnTable) {
		synchronized (betLock) {
			this.betOnTable += betOnTable;
		}
	}

	/**
	 * @param betOnTable the betOnTable to set
	 */
	public void setBetOnTable(double betOnTable) {
		this.betOnTable = betOnTable;
	}
	/**
	 * @return the player
	 */
	public Player getPlayer() {
		return player;
	}

	/**
	 * Method for removing all cards on table.
	 */
	public void removeAllCards() {
		synchronized (cards) {
			cards.removeAll(cards);
		}
	}
	/**
	 * Method for adding a single card to the table.
	 * @param card to add
	 */
	public void addCard(Card card) {
		synchronized (cards) {
			cards.add(card);
		}
	}
	/**
	 * Remove a single card from table.
	 * @param card to remove
	 */
	public void removeCard(Card card) {
		synchronized (cards) {
			cards.remove(card);
		}
	}
	/**
	 * Get all the cards on table.
	 * @return All the cards on table.
	 */
	public ArrayList<Card> getAllCardsOnTable() {
		synchronized (cards) {
			return this.cards;
		}
	}
	/**
	 * Method to use if cards are wanted. Must be synchronized outside!
	 * @return the cards arraylist iterator.
	 */
	public Iterator<Card> cardsIterator() {
		return cards.iterator();
	}
	
}
